<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Administration extends Base
{
	
	public function index()
	{
		return $this->fetch();
	}
	/*成衣库存列表-----行政库存*/
	public function clothesList()
	{	
		if(Request::instance()->isPost())
		{
			$name = Request::instance()->post('name');
			$category = Request::instance()->post('category');
			if($category > -1)
			{
				$where['category']  = $category;
			}
			if(strlen($name) > 10)
			{
				$where['code'] = array('like',"%$name%");
			}else{
				$where['ml_num'] = array('like',"%$name%");
			}
			 /*$aa = preg_match('/[\x{4e00}-\x{9fa5}]/u', $name);//判断里面有没有汉字
                if($aa == 1){//说明有汉字，那就是暗名称搜
                    //$where .=" and title like '%".$name."%'";
                    $code = DB::name('clothes_code')->field('code')->where("name like '%".$name."%'")->select();
                   $arr = "";
                    foreach ($code as $key => $value) {
                    	$arr .= $value['code'].',';
                    }
                    $arr = substr($arr, 0 ,-1);
                    $where['code'] = array('in',$arr);	
                }else{//没有就是面料编号
                    $where['ml_num'] = array('like',"%$name%");
                }*/

		}
		$where['is_del'] = 0;
		$list = DB::name('xingzheng_kucun')->where($where)->order('id desc')->paginate(15);
		$count = DB::name('xingzheng_kucun')->where($where)->sum('num');
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			$code = DB::name('clothes_code')->where("code = '".$v['code']."'")->find();
			$list[$k]['name'] = $code['name'];
			$list[$k]['price'] = $code['price'];
			$list[$k]['ml_num'] = $code['ml_num'];
			$list[$k]['ml_style'] = $code['ml_style'];
			switch ($code['season']) {
				case '1':
				$list[$k]['season'] = '春季';
				break;
				case '2':
				$list[$k]['season'] = '夏季';
				break;
				case '3':
				$list[$k]['season'] = '秋季';
				break;
				case '4':
				$list[$k]['season'] = '冬季';
				break;	
				
			}
			$list[$k]['category'] = DB::name('clothes_category')->where('id ='.$code['category'])->value('value');
			$list[$k]['color']	  = DB::name('clothes_color')->where('id = ' .$code['color'])->value('value');
			$list[$k]['size']     = DB::name('clothes_size')->where('id = '.$code['size'])->value('value');	
		}
		$this->assign('category',DB::name('clothes_category')->order('id asc')->select());
		return $this->fetch('clothes_list',[
			'list'		=>	$list,
			'page'		=>	$page,
			'count'		=>	$count,
		]);
	}

	/*分配成衣*/
	public function fenClothes($id)
	{
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'xing_fenpei');
		if($auth !=true){//没有删除菜单的权限
				$this->error('权限不足');
			}	
		/*添加成衣记录*/
		if(request::instance()->isPost())
		{
			$fenpei_num = request::instance()->post('fenpei_num');
			$data['name'] = request::instance()->post('name');
			$data['code'] = request::instance()->post('code');
			$data['ml_num'] = request::instance()->post('ml_num');
			$data['ml_style'] = request::instance()->post('ml_style');
			$data['entry_id'] = Session::get('manage_id');
			$data['addtime'] = time();
			$shop = request::instance()->post('shopid/a');
			$fenpei = request::instance()->post('fenpei/a');
							
			/*分配正常的情况下，在行政分配表中插入数据*/
				for ($i=0; $i < count($fenpei); $i++) { 
					$data['shopid'] = $shop[$i];
					$data['num'] = $fenpei[$i];
					$res = DB::name('xingzheng_fenpei')->insert($data);
				}
				$resault = DB::name('xingzheng_kucun')->where('id ='.$id)->setField('fenpei_num',$fenpei_num);
				if($resault){
				$this->success('分配成功',url('index/index/index',array('con'=>'Administration','act'=>'clothesList')));
				}
		}
		/*获取信息数据*/
		$data = DB::name('xingzheng_kucun')->where('id = '.$id)->find();
		$code = DB::name('clothes_code')->where("code ='".$data['code']."'")->find();
		$data['name'] = $code['name'];
		$data['ml_num'] = $code['ml_num'];
		$data['ml_style'] = $code['ml_style'];
		$shop = DB::name('shop')->where('status = 1 and is_del = 0 ')->order('orderid asc')->select();
		return $this->fetch('fen_clothes',[
			'data'		=>	$data,
			'shop'		=>	$shop,
		]);
	}

	/*分配成衣记录列表*/
	public function fenpei()
	{
		if(request::instance()->isPost())/*查询条件：根据店铺名称查询*/
		{
			$data = request::instance()->post('name');
			$shop = DB::name('shop')->field('id')->where("title like '%".$data."%'")->select();
			$arr = '';
			foreach ($shop as $k => $v) {
				$arr .= $v['id'].',';
			}
			$shop = substr($arr, 0,-1);
			
			$where['shopid'] = array('in',$shop);
		}
		$where['is_del'] = 0;
		$list = DB::name('xingzheng_fenpei')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			/*获取店铺名称*/
			$list[$k]['shop']  = DB::name('shop')->where('id = '.$v['shopid'])->value('title');
			/*获取分配者名称*/
			$list[$k]['entry'] = DB::name('user')->where('id = '.$v['entry_id'])->value('truename');
			/*获取价格和型号*/
			$list[$k]['price'] = DB::name('clothes_code')->where("code ='".$v['code']."'")->value('price');
			$list[$k]['size'] = DB::name('clothes_size')->where('id = '.DB::name('clothes_code')->where("code ='".$v['code']."'")->value('size'))->value('value');
		}
		return $this->fetch('fenpei',[
			'list'		=>	$list,
			'page'		=>	$page,
		]);
	}
	/*添加成衣库存*/
	public function addClothes()
	{
		$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'xing_add');
			if($auth !=true){//没有删除菜单的权限
				$this->error('权限不足');
		}
		return $this->fetch('add_clothes');
	}
    /*先查询产品信息 然后在统一录入*/
    public function getInfoForAjax()
    {
        if(Request::instance()->isPost())
        {
            $where['code'] = request::instance()->post('order');
            $where['is_del'] = 0 ;
            $data = DB::name('clothes_code')->where($where)->find();
            if($data){
                switch ($data['season']) {
                    case '1':
                        $data['season'] = '春季';
                        break;
                    case '2':
                        $data['season'] = '夏季';
                        break;
                    case '3':
                        $data['season'] = '秋季';
                        break;
                    case '4':
                        $data['season'] = '冬季';
                        break;
                }
                $data['category'] = DB::name('clothes_category')->where('id ='.$data['category'])->value('value');
                $data['color']	  = DB::name('clothes_color')->where('id = ' .$data['color'])->value('value');
                $data['size']     = DB::name('clothes_size')->where('id = '.$data['size'])->value('value');
                $data['sus'] = 1;
            }else{
                $data['sus'] = 2;
            }
            return json($data);
        }
    }
	/*ajax添加库存->扫码枪扫描*/
	public function kucunAjax()
	{
		if(request::instance()->isPost())
		{
            $code   =   request::instance()->post('code/a');
            $num    =   request::instance()->post('num/a');
            $entry  =   Session::get('manage_id');
            DB::startTrans();
            try{
                for($i = 0; $i< count($code); $i++){
                    $cunzai = DB::name('xingzheng_kucun')->field('id,num,shengyu_num,fenpei_num')->where("is_del = 0 and code = '".$code[$i]."'")->find();
                    if(count($cunzai) > 0){
                        $all_num        =   DB::name('xingzheng_kucun')->where("is_del = 0 and code = '".$code[$i]."'")->setInc('num',$num[$i]);
                        $shengyu_num    =   DB::name('xingzheng_kucun')->where("is_del = 0 and code = '".$code[$i]."'")->setInc('shengyu_num',$num[$i]);
                        $fenpei_num     =   DB::name('xingzheng_kucun')->where("is_del = 0 and code = '".$code[$i]."'")->setInc('fenpei_num',$num[$i]);
                    }else{
                        $data['code']   =   $code[$i];
                        $data['num'] = $data['shengyu_num'] =  $data['fenpei_num'] =$num[$i];
                        $data['entry_id'] = $entry;
                        $data['category'] = DB::name('clothes_code')->where("code = '".$code[$i]."' and is_del = 0")->value('category');
                        $data['ml_num']	=	DB::name('clothes_code')->where("code = '".$code[$i]."' and is_del = 0")->value('ml_num');
                        $data['addtime'] =  time();
                        $res    =   DB::name('xingzheng_kucun')->insert($data);
                    }
                }
             DB::commit();
            }catch (\Exception $e){
                DB::rollback();
                $this->error('入库失败，请检查问题后重新录入');
            }
            $this->success('入库成功',url('index/index/index',array('con'=>'Administration','act'=>'clothesList')));
		}
	}

	/*删除行政库存列表 -----只修改状态 不删除数据*/
	public function shanchu()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'xing_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('xingzheng_kucun')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}
	/*删除行政分配列表 -----只修改状态 不删除数据*/
	public function shanchufenpei()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'xing_delfenpei');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('xingzheng_fenpei')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}
}
?>